package com.mxx.basic.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.gyf.immersionbar.ImmersionBar;
import com.mxx.basic.utils.Logger;
import com.yp.loading.interfaces.ILoadingView;
import com.yp.loading.widget.LoadingDialog;

import java.io.Serializable;

import me.jessyan.autosize.AutoSizeCompat;

/**
 * Activity的基类
 */
public abstract class BaseActivity extends AppCompatActivity implements ActivityCallback {
    protected Activity targetActivity;
    protected String tag;
    protected ImmersionBar mImmersionBar;
    protected Logger mLogger;
    protected ILoadingView loading_dialog;

    @Override
    public void getTargetActivity() {
        targetActivity = this;
    }

    @Override
    public void setTag(String tag) {
        this.tag = tag;
    }

    @Override
    public void removeActionBar(Activity activity) {
        if (activity == null) {
            return;
        }
        if (activity instanceof AppCompatActivity) {
            //第一种方法：
            ((AppCompatActivity) activity).supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
            ((AppCompatActivity) activity).getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            //第二种:hideActionBar(AppCompatActivity activity)
        } else {
            activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
    }

    @Override
    public void setStatusBarColor(Activity activity) {
        int color = getStatusBarColor();
        boolean lightColor = isLightColor(color);
        mImmersionBar = ImmersionBar.with(this);
        //做自己的逻辑
        mImmersionBar
                .fitsSystemWindows(fitsSystemWindow())
                .statusBarColor(color)
                .statusBarDarkFont(!lightColor)
                .keyboardEnable(true)
                .setOnKeyboardListener(this::doSomething);
        mImmersionBar.init();
    }

    protected void doSomething(boolean isPopup, int keyboardHeight) {
    }

    protected int getStatusBarColor() {
        return android.R.color.white;
    }

    /**
     * 是否是亮色
     *
     * @param color int
     * @return boolean
     */
    public boolean isLightColor(int color) {
        double darkness = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255;
        // It's a dark color
        return darkness < 0.5; // It's a light color
    }

    protected boolean fitsSystemWindow() {
        return true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLogger = Logger.getInstance();
        mLogger.i(tag, "onCreate");
        loading_dialog = new LoadingDialog();
    }

    @Override
    public abstract void initView();

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {

    }


    @Override
    protected void onStart() {
        super.onStart();
        mLogger.i(tag, "onStart");
    }

    @Override
    protected void onResume() {
//        /**
//         * 设置为横屏
//         */
//        if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//        }
        super.onResume();
        mLogger.i(tag, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        mLogger.i(tag, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        mLogger.i(tag, "onStop");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mLogger.i(tag, "onRestart");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLogger.i(tag, "onDestroy");
    }

    protected Serializable getSerValueByKey(String key) {
        if ("".equals(key) || key == null) {
            return null;
        }
        Intent mIntent = getIntent();
        if (mIntent == null) {
            return null;
        }
        Bundle mBundle = mIntent.getExtras();
        if (mBundle == null) {
            return null;
        }
        Serializable serializable = null;

        try {
            serializable = mBundle.getSerializable(key);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return serializable;
    }

    @Override
    public Resources getResources() {
        AutoSizeCompat.autoConvertDensityOfGlobal(super.getResources());
        return super.getResources();
    }


}